package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcServiceSecondLevelMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcServiceFirstLevel;
import com.csun.cmny.provider.model.domain.OmcServiceSecondLevel;
import com.csun.cmny.provider.model.domain.OmcServiceThirdLevel;
import com.csun.cmny.provider.model.dto.service.ServiceSecondLevelSelectDto;
import com.csun.cmny.provider.model.vo.service.ServiceSecondLevelVo;
import com.csun.cmny.provider.service.OmcServiceSecondLevelService;
import com.csun.cmny.provider.service.OmcServiceThirdLevelService;
import com.csun.cmny.provider.utils.AppUtil;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Transactional
@Service
public class OmcServiceSecondLevelServiceImpl extends BaseService<OmcServiceSecondLevel> implements OmcServiceSecondLevelService {

    @Resource
    private OmcServiceSecondLevelMapper omcServiceSecondLevelMapper;
    @Resource
    private OmcServiceThirdLevelService omcServiceThirdLevelService;

    @Override
    public Integer saveServiceSecondLevel(OmcServiceSecondLevel omcServiceSecondLevel, LoginAuthDto loginAuthDto) {

        //校验名称
        checkNameExsit(omcServiceSecondLevel, loginAuthDto);

        if (PublicUtil.isEmpty(omcServiceSecondLevel.getId())) {
            return omcServiceSecondLevelMapper.insertSelective(omcServiceSecondLevel);
        } else {
            return omcServiceSecondLevelMapper.updateByPrimaryKeySelective(omcServiceSecondLevel);
        }
    }

    @Override
    public int deleteSecondLevel(Long secondLevelId) {

        OmcServiceThirdLevel serviceThirdLevelQuery = new OmcServiceThirdLevel();
        serviceThirdLevelQuery.setSencondLevelId(secondLevelId);
        int i = omcServiceThirdLevelService.selectCount(serviceThirdLevelQuery);

        if (i > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080005);
        }
        return omcServiceSecondLevelMapper.deleteByPrimaryKey(secondLevelId);
    }

    @Override
    public List<ServiceSecondLevelVo> selectServiceLevel(ServiceSecondLevelSelectDto serviceSecondLevelSelectDto) {
        return omcServiceSecondLevelMapper.selectServiceLevel(serviceSecondLevelSelectDto);
    }

    private void checkNameExsit(OmcServiceSecondLevel omcServiceSecondLevel, LoginAuthDto loginAuthDto) {
        Example example = new Example(OmcServiceSecondLevel.class);
        Example.Criteria criteria = example.createCriteria();
//        if (PublicUtil.isNotEmpty(omcServiceSecondLevel.getId())) {
//            criteria.andNotEqualTo("id", omcServiceSecondLevel.getId());
//        }

        if (!AppUtil.isNullOrEmpty(loginAuthDto.getGroupId().toString())) {
            criteria.andEqualTo("groupId", loginAuthDto.getGroupId());
        }

        criteria.andEqualTo("secondLevelName", omcServiceSecondLevel.getSecondLevelName());
        int i = selectCountByExample(example);
        if (i > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080004);
        }
    }
}
